<template>
  <skeleton v-if="showSkeleton"></skeleton>
  <view class="viewport" v-else>
    <!-- 搜索框 -->
    <view class="search">
      <view class="input">
        <text class="icon-search" @click="onSearch"></text>
        <input v-model="keyword" placeholder="女靴" style="flex: 1" />
      </view>
    </view>
    <!-- 分类 -->
    <view class="categories">
      <!-- 左侧：一级分类 -->
      <scroll-view class="primary" scroll-y>
        <view
          v-for="(item, index) in categoryList"
          :key="item.id"
          class="item"
          :class="{ active: index === currentIndex }"
          @click="clickOneCategory(index)"
        >
          <text class="name"> {{ item.name }} </text>
        </view>
      </scroll-view>
      <view class="secondary">
        <!-- 焦点图 -->
        <tphSwiper :homeBannerList="bannerList" height="220rpx" />
        <!-- 右侧：二级分类 -->
        <scroll-view scroll-y :scroll-top="scrollTop" @scroll="onScroll" class="secondary_scroll">
          <!-- 内容区域 -->
          <view class="panel" v-for="item in twoCategoryList" :key="item.id">
            <view class="title">
              <text class="name">{{ item.name }}</text>
              <navigator class="more" hover-class="none">全部</navigator>
            </view>
            <view class="section">
              <navigator
                v-for="goods in item.goods"
                :key="goods.id"
                class="goods"
                hover-class="none"
                :url="`/pages/goodsDetail/goodsDetail?id=${goods.id}`"
              >
                <image class="image" :src="goods.picture"></image>
                <view class="name ellipsis">{{ goods.name }}</view>
                <view class="price">
                  <text class="symbol">¥</text>
                  <text class="number">{{ goods.price }}</text>
                </view>
              </navigator>
            </view>
          </view>
        </scroll-view>
      </view>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
// 引入接口函数
import { getCategoryAPI } from '@/api/category'
import { getHomeBannerAPI } from '@/api/home'
import { getSearchAPI } from '@/api/search'
// 引入ts类型
import type { categoryListType, childrenType } from '@/types/category'
import type { bannerInfoType } from '@/types/home'
// 引入骨架屏
import skeleton from './components/skeleton.vue'
// 控制显示骨架屏
const showSkeleton = ref(true)
// 商品分类列表
const categoryList = ref<categoryListType[]>([] as categoryListType[])
// 二级分类列表
const twoCategoryList = ref<childrenType[]>([] as childrenType[])
// 轮播图列表
const bannerList = ref<bannerInfoType[]>([] as bannerInfoType[])
// 当前一级分类的索引
const currentIndex = ref(0)
// 获取商品分类数据的函数
const getCategoryList = async () => {
  const res = await getCategoryAPI()
  if (res.code === '1') {
    categoryList.value = res.result
    // 默认获取第一个一级分类的二级分类
    twoCategoryList.value = categoryList.value[0].children || []
  }
}
// 滚动容器距离顶部的距离
const scrollTop = ref(0)
// 获取轮播图数据的函数
const getHomeBannerList = async () => {
  const res = await getHomeBannerAPI(2)
  if (res.code === '1') {
    bannerList.value = res.result
  }
}
// 点击一级分类的回调
const clickOneCategory = (index: number) => {
  currentIndex.value = index
  // 重新赋值二级分类
  twoCategoryList.value = categoryList.value[index].children || []
  // 重新让滚动容器回到顶部
  scrollTop.value = 0
}
const onScroll = (e: any) => {
  scrollTop.value = e.target.scrollTop
}
// 搜索关键词
const keyword = ref('')
// 当前页数
const page = ref(1)
// 每页大小
const pageSize = ref(10)
// 搜索图标的回调
const onSearch = () => {
  getSearchData()
}
// 获取搜索商品的函数
const getSearchData = async () => {
  const data = {
    keyword: keyword.value,
    page: page.value,
    pageSize: pageSize.value,
  }
  // 调用接口
  const res = await getSearchAPI(data)
  console.log(res)
}
onLoad(async () => {
  showSkeleton.value = true
  await Promise.all([getCategoryList(), getHomeBannerList()])
  showSkeleton.value = false
})
</script>

<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
}
.viewport {
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.search {
  padding: 20rpx;
  background-color: #fff;
  .input {
    display: flex;
    align-items: center;
    height: 64rpx;
    padding-left: 26rpx;
    color: #8b8b8b;
    font-size: 28rpx;
    border-radius: 32rpx;
    background-color: #f3f4f4;
  }
}
.icon-search {
  &::before {
    margin-right: 10rpx;
  }
}
/* 分类 */
.categories {
  flex: 1;
  min-height: 400rpx;
  display: flex;
}
/* 一级分类 */
.primary {
  overflow: hidden;
  width: 180rpx;
  flex: none;
  background-color: #f6f6f6;
  .item {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 96rpx;
    font-size: 26rpx;
    color: #595c63;
    position: relative;
    &::after {
      content: '';
      position: absolute;
      left: 42rpx;
      bottom: 0;
      width: 96rpx;
      border-top: 1rpx solid #e3e4e7;
    }
  }
  .active {
    background-color: #fff;
    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
      width: 8rpx;
      height: 100%;
      background-color: #27ba9b;
    }
  }
}
.primary .item:last-child::after,
.primary .active::after {
  display: none;
}
/* 二级分类 */
.secondary {
  background-color: #fff;
  height: 100%;
  display: flex;
  flex-direction: column;
  .secondary_scroll {
    flex: 1;
    overflow-y: scroll;
    height: calc(100% - var(--window-bottom));
    .carousel {
      height: 200rpx;
      margin: 0 30rpx 20rpx;
      border-radius: 4rpx;
      overflow: hidden;
    }
    .panel {
      margin: 0 30rpx 0rpx;
    }
    .title {
      height: 60rpx;
      line-height: 60rpx;
      color: #333;
      font-size: 28rpx;
      border-bottom: 1rpx solid #f7f7f8;
      .more {
        position: relative;
        float: right;
        padding-left: 20rpx;
        padding-right: 20rpx;
        font-size: 24rpx;
        color: #999;
      }
    }
    .more {
      &::after {
        position: absolute;
        top: 3rpx;
        right: 0rpx;
        font-family: 'iconfont' !important;
        content: '\e612';
        font-size: 16rpx;
      }
    }
    .section {
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      padding: 20rpx 0;
      .goods {
        width: 150rpx;
        margin: 0rpx 30rpx 20rpx 0;
        &:nth-child(3n) {
          margin-right: 0;
        }
        image {
          width: 150rpx;
          height: 150rpx;
        }
        .name {
          padding: 5rpx;
          font-size: 22rpx;
          color: #333;
        }
        .price {
          padding: 5rpx;
          font-size: 18rpx;
          color: #cf4444;
        }
        .number {
          font-size: 24rpx;
          margin-left: 2rpx;
        }
      }
    }
  }
}
</style>
